package com.cddata;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import com.cddata.util.DBOperation;

@SuppressWarnings("serial")
public class SearchPane extends JPanel implements ActionListener{
	public SearchPane() {
		display();
	}

	private void display() {
		JPanel searchPanel = new JPanel();
		searchPanel.setLayout(new BoxLayout(searchPanel , BoxLayout.Y_AXIS));
		searchPanel.add(DBOperation.getHelperBean().createHeaderPanel("Search Cds ...", null));
		searchPanel.add(createSearchPanel());
		searchPanel.add(createButtonPanel());
		searchPanel.add(new FileUpload());
		add(searchPanel);
		setVisible(true);
	}
	
	@SuppressWarnings("unchecked")
	private JPanel createSearchPanel () {
		JPanel searchPanel = new JPanel();
		searchPanel.add( new JLabel("Keyword : "));
		searchPanel.add(searchField);
		// inserting null for showing blank 
		types.addItem("Select");
		HashMap<String ,String > cTypes = DBOperation.getHelperBean().contentTypes;
		Collection list = cTypes.values();
	
		for( Iterator<String> it = list.iterator(); it.hasNext();) {
			String typeName = it.next();
			types.addItem(typeName);
		}
		searchPanel.add( types);
		return searchPanel;
	}
	
	private JPanel createButtonPanel () {
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBorder(new TitledBorder(""));
		JButton search = new JButton("Search");
		JButton reset= new JButton("Reset");
		
		search.addActionListener(this);
		reset.addActionListener(this);
		
		buttonPanel.add( search);
		buttonPanel.add( reset);
		
		return buttonPanel;
	}
	
	public void actionPerformed( ActionEvent ae ) {
		String action = ae.getActionCommand();
		if("Search".equalsIgnoreCase(action)) {
			DBOperation.getHelperBean().hideContainer();
			SearchResult searchResult = new SearchResult();
			if(types.getSelectedIndex()!= -1)
			searchResult.setTypeName(types.getSelectedItem().toString());
			searchResult.setSearchStr(searchField.getText());
			searchResult.display();
			DBOperation.getHelperBean().addContainer(searchResult);
		}
	}
	
	JTextField searchField = new JTextField(15);
	JComboBox types = new JComboBox();
}

